//
//  MenuLayer.m
//  618
//
//  Created by Ling on 13-6-21.
//  Copyright 2013年 __618工作室__. All rights reserved.
// 

#import "MenuLayer.h"
#import "AppDelegate.h"

@implementation MenuLayer

@synthesize menu, delegate;
@synthesize background;

-(id) init
{
	self = [super init];
	
	if (self)
	{
		CGSize size = [[CCDirector sharedDirector] winSize];
        // Load background
       // NSString *bgSheet = @"bg.png";
        Log_Debug(@"加载菜单");
        
        NSString *fileName;
        if (IS_Ipad) {
           fileName = @"bg2.png";
        }
        else
        {
            fileName = @"bg2.jpg";
        }
        
        CCSprite *back = [CCSprite spriteWithSpriteFrameName:fileName];
        back.position = ccp(size.width/2,size.height/2);
		self.background = back;
		
		[self addChild:background];
        
        if (IS_Ipad) {
            [CCMenuItemFont setFontSize:45];
        }
        else
        {
            [CCMenuItemFont setFontSize:25];
        }
		
		[CCMenuItemFont setFontName:@"ArialRoundedMTBold"];//TrebuchetMS-Bold,SnellRoundhand-Bold Thonburi-Bold ,Marker Felt,GeezaPro-Bold 
        
		
        //+(id) itemWithNormalImage: (NSString*)value selectedImage:(NSString*) value2 target:(id) r selector:(SEL) s;
        
        CCMenuItem *startGames = [CCMenuItemFont itemWithString:@"开始新游戏"
												target:self
											  selector:@selector(restartGame:)];
        
        //startGames.
		
		CCMenuItem *continueGames = [CCMenuItemFont itemWithString:@"继续游戏"
                                                    target:self
                                                  selector:@selector(continueGames:)];
        
        
		
		CCMenuItem *scores = [CCMenuItemFont itemWithString:@"成绩榜"
                                                 target:self
                                               selector:@selector(highScores:)];
		
		CCMenuItem *about = [CCMenuItemFont itemWithString:@"关于我们"
                                                target:self
                                              selector:@selector(about:)];
        

        
//        // Achievement Menu Item using blocks
//		CCMenuItem *itemAchievement = [CCMenuItemFont itemWithString:@"成就榜" block:^(id sender) {
//			
//			
//			GKAchievementViewController *achivementViewController = [[GKAchievementViewController alloc] init];
//			achivementViewController.achievementDelegate = self;
//			
//			AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
//			
//			[[app navController] presentModalViewController:achivementViewController animated:YES];
//			
//			[achivementViewController release];
//		}
//									   ];
//        
//		// Leaderboard Menu Item using blocks
//		CCMenuItem *itemLeaderboard = [CCMenuItemFont itemWithString:@"英雄榜" block:^(id sender) {
//			
//			
//			GKLeaderboardViewController *leaderboardViewController = [[GKLeaderboardViewController alloc] init];
//			leaderboardViewController.leaderboardDelegate = self;
//			
//			AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
//			
//			[[app navController] presentModalViewController:leaderboardViewController animated:YES];
//			
//			[leaderboardViewController release];
//		}
//        									   ];
//
		

//		menu = [CCMenu menuWithItems:startGames, [MenuLayer getSpacerItem], continueGames, [MenuLayer getSpacerItem], scores, [MenuLayer getSpacerItem],itemAchievement, [MenuLayer getSpacerItem],itemLeaderboard, [MenuLayer getSpacerItem], about, nil];
		menu = [CCMenu menuWithItems:startGames, [MenuLayer getSpacerItem], continueGames, [MenuLayer getSpacerItem], scores,[MenuLayer getSpacerItem], about, nil];

        [menu alignItemsVertically];

        [self addChild:menu];
	}
	
	return self;
}

#pragma mark <Selector Methods>
- (void) about:(id) sender
{
	[[NSNotificationCenter defaultCenter] postNotificationName:@"about" object:@""];
}

- (void) highScores:(id) sender
{
	[[NSNotificationCenter defaultCenter] postNotificationName:@"highScores" object:@""];
}

- (void) continueGames:(id) sender
{
	if ([self.delegate respondsToSelector:@selector(restartGame)])
	{
		[self.delegate continueGames];
	}
}

#pragma mark <Delegate Methods>
- (void) restartGame:(id) sender
{
	if ([self.delegate respondsToSelector:@selector(restartGame)])
	{
		[self.delegate restartGame];
	}
}

#pragma mark <add a spacer item after every selector>

+ (CCMenuItemFont *) getSpacerItem
{
	[CCMenuItemFont setFontSize:10];
	return [CCMenuItemFont itemWithString:@" " target:self selector:nil];
}

- (void) dealloc
{
	[background release];
	[delegate release];
	[menu release];
	[super dealloc];
}
#pragma mark GameKit delegate

-(void) achievementViewControllerDidFinish:(GKAchievementViewController *)viewController
{
	AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
	[[app navController] dismissModalViewControllerAnimated:YES];
}

-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
	AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
	[[app navController] dismissModalViewControllerAnimated:YES];
}

@end
